import type { RouteLocationNormalized, RouteRecordNormalized } from 'vue-router';
import type { App, Plugin } from 'vue';

import { unref } from 'vue';
import { isObject } from '/@/utils/is';

export const noop = () => {};

/**
 * @description:  Set ui mount node
 */
export function getPopupContainer(node?: HTMLElement): HTMLElement {
  return (node?.parentNode as HTMLElement) ?? document.body;
}

/**
 * Add the object as a parameter to the URL
 * @param baseUrl url
 * @param obj
 * @returns {string}
 * eg:
 *  let obj = {a: '3', b: '4'}
 *  setObjToUrlParams('www.baidu.com', obj)
 *  ==>www.baidu.com?a=3&b=4
 */
export function setObjToUrlParams(baseUrl: string, obj: any): string {
  let parameters = '';
  for (const key in obj) {
    parameters += key + '=' + encodeURIComponent(obj[key]) + '&';
  }
  parameters = parameters.replace(/&$/, '');
  return /\?$/.test(baseUrl) ? baseUrl + parameters : baseUrl.replace(/\/?$/, '?') + parameters;
}

// 深度合并
export function deepMerge<T = any>(src: any = {}, target: any = {}): T {
  let key: string;
  for (key in target) {
    src[key] = isObject(src[key]) ? deepMerge(src[key], target[key]) : (src[key] = target[key]);
  }
  return src;
}

export function openWindow(
  url: string,
  opt?: { target?: TargetContext | string; noopener?: boolean; noreferrer?: boolean },
) {
  const { target = '__blank', noopener = true, noreferrer = true } = opt || {};
  const feature: string[] = [];

  noopener && feature.push('noopener=yes');
  noreferrer && feature.push('noreferrer=yes');

  window.open(url, target, feature.join(','));
}
//
// // dynamic use hook props
// export function getDynamicProps<T, U>(props: T): Partial<U> {
//   const ret: Recordable = {};
//
//   Object.keys(props).map((key) => {
//     ret[key] = unref((props as Recordable)[key]);
//   });
//
//   return ret as Partial<U>;
// }
//
// export function getRawRoute(route: RouteLocationNormalized): RouteLocationNormalized {
//   if (!route) return route;
//   const { matched, ...opt } = route;
//   return {
//     ...opt,
//     matched: (matched
//       ? matched.map((item) => ({
//           meta: item.meta,
//           name: item.name,
//           path: item.path,
//         }))
//       : undefined) as RouteRecordNormalized[],
//   };
// }
//
// export const withInstall = <T>(component: T, alias?: string) => {
//   const comp = component as any;
//   comp.install = (app: App) => {
//     app.component(comp.name || comp.displayName, component);
//     if (alias) {
//       app.config.globalProperties[alias] = component;
//     }
//   };
//   return component as T & Plugin;
// };

//数字格式化 10000 =》 10,000
export const toThousands = (num = 0) => {
  return num.toString().replace(/\d+/, function(n) {
    return n.replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
  });
}



//后端未做时间格式化的对象格式化
export function formatToDateTime(date:any){
  if (date.monthValue<10){
    date.monthValue = '0' + date.monthValue
  }
  if (date.dayOfMonth<10){
    date.dayOfMonth = '0' + date.dayOfMonth
  }
  if (date.hour<10){
    date.hour = '0' + date.hour
  }
  if (date.minute<10){
    date.minute = '0' + date.minute
  }
  if (date.second<10){
    date.second = '0' + date.second
  }
  return `${date.year}-${date.monthValue}-${date.dayOfMonth} ${date.hour}:${date.minute}:${date.second}`;
}


export function base64toFile(base64:string, filename:string):File {
  //将base64转换为文件
  let arr = base64.split(','),
      mime = arr[0].match(/:(.*?);/)![1],
      bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
  while(n--){
    u8arr[n] = bstr.charCodeAt(n);
  }
  return new File([u8arr], filename, {type:mime});
}

export function uuid() {
  var s = [];
  var hexDigits = "0123456789abcdef";
  for (var i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
  // @ts-ignore
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  s[8] = s[13] = s[18] = s[23] = "-";

  var uuid = s.join("");
  return uuid;
}
